package com.example.module_word.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.module_word.R;
import com.example.module_word.room.Danci;
import com.example.module_word.room.entity.MyWord;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils;
import java.util.List;

public class LexiconRvAdapter extends RecyclerView.Adapter<LexiconRvAdapter.ViewHolder> {
    private List<Danci> list;
    private List<MyWord> myWordList;
    private int type=0;

    private OnListClickListener<String> onListClickListener;
    public LexiconRvAdapter(List<Danci> list) {
        this.list = list;
    }
    public LexiconRvAdapter(List<MyWord> myWordList, int type) {
        this.myWordList = myWordList;
        this.type = type;
    }

    public void setOnListClickListener(OnListClickListener<String> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_lexicon,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String word,phonetic,desc;
        if (type!=1){
            word = list.get(position).word;
            phonetic = list.get(position).phonetic;
            desc = list.get(position).desc;
        }else {
            word = myWordList.get(position).word;
            phonetic = myWordList.get(position).phonetic;
            desc = myWordList.get(position).desc;
        }
        holder.word.setText(word);
        holder.phonetic.setText(phonetic);
        holder.hint.setText(desc);
        holder.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (QuickClickUtils.isFastClick()){
                    return;
                }
                if (onListClickListener!=null){
                    onListClickListener.itemClick(position,word);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return type==1?myWordList.size(): list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView word,phonetic,hint;
        private ImageView play;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            word = itemView.findViewById(R.id.word);
            phonetic = itemView.findViewById(R.id.phonetic);
            hint = itemView.findViewById(R.id.hint);
            play = itemView.findViewById(R.id.play);
        }
    }
}
